package com.egreen.system.service.api.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.common.pojo.DTO.system.*;
import com.egreen.common.pojo.VO.system.GetUserFeedbackVO;
import com.egreen.common.pojo.entity.system.*;
import com.egreen.common.pojo.VO.system.CommodityExchangeRecordVO;
import com.egreen.system.dao.*;
import com.egreen.system.ex.SystemException;
import com.egreen.common.pojo.VO.system.GetSystemAnnouncementVO;
import com.egreen.common.pojo.VO.system.GetSystemInformationVO;
import com.egreen.common.serviceapi.system.ISystemAPI;
import com.egreen.system.service.ICommodityService;
import com.egreen.system.service.impl.CommodityExchangeRecordServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SystemAPIImpl implements ISystemAPI {
    @Autowired
    SystemAnnouncementDao announcementDao;

    @Autowired
    SystemInformationDao systemInformationDao;
    @Autowired
    ICommodityService commodityService;
    @Autowired
    UserFeedbackDao userFeedbackDao;

    @Autowired
    CommodityExchangeRecordServiceImpl commodityExchangeRecordService;

    @Autowired
    CommodityDao commodityDao;

    @Autowired
    CommodityExchangeRecordDao commodityExchangeRecordDao;

    /**
     * 兑换记录的查找
     *
     * @return
     */
    @Override
    public List<CommodityExchangeRecordVO> selectExchangeRecords(int state) {
        //0表示未填写订单，1表示已填写订单
        if (state == 0) {
            QueryWrapper queryWrapper = new QueryWrapper();
            //当state为0时，除了id，返回的所有值都为空
            queryWrapper.select("id", "exchange_time", "user_id", "commodity_id", "state", "logistics_information");
            //进行筛选
            queryWrapper.eq("state", state);
            //根据条件查询得到数据封装到集合里
            List<CommodityExchangeRecordVO> list = commodityExchangeRecordDao.selectList(queryWrapper);
            return list;
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select("id", "exchange_time", "user_id", "commodity_id", "state", "logistics_information");
            //进行筛选
            queryWrapper.eq("state", state);
            //根据条件查询得到数据封装到集合里
            List<CommodityExchangeRecordVO> list = commodityExchangeRecordDao.selectList(queryWrapper);
            return list;
        }
    }

    /**
     * 系统公告查看
     *
     * @return
     */
    @Override
    public List<GetSystemAnnouncementVO> getAllSystemAnnouncement() {
        QueryWrapper wrapper = new QueryWrapper();
        //添加我要查询的字段
        wrapper.select("id", "title", "content", "release_time");
        //查询
        List<GetSystemAnnouncementVO> list = announcementDao.selectList(wrapper);
        return list;
    }


    /**
     * 添加系统公告
     */
    @Override
    public void addSystemAnnouncement(SystemAnnouncementDTO systemAnnouncementDTO) {
        SystemAnnouncement systemAnnouncement = new SystemAnnouncement();
        systemAnnouncement.setId(systemAnnouncementDTO.getId());
        systemAnnouncement.setTitle(systemAnnouncementDTO.getTitle());
        systemAnnouncement.setContent(systemAnnouncementDTO.getContent());
        int insert = announcementDao.insert(systemAnnouncement);

        if (insert != 1) {
            //插入失败，抛出异常
            throw new SystemException("插入失败");
        }
    }

    /**
     * 删除系统公告
     */
    @Override
    public void deleteSystemAnnouncement(int id) {
        int delete = announcementDao.deleteById(id);
        if (delete != 1) {
            //删除失败，抛出异常
            throw new SystemException("删除失败");
        }
    }


    /**
     * 用户查看自己的  系统信息
     *
     * @return
     */

    @Override
    public List<GetSystemInformationVO> getSystemInformation(int userId) {

        QueryWrapper wrapper = new QueryWrapper();
        //查询的内容
        wrapper.select("id", "send_time", "title", "content", "state");
        //查询条件
        wrapper.eq("user_id", userId);
        List list = systemInformationDao.selectList(wrapper);

        return list;
    }


    /**
     * 添加系统信息
     */
    @Override
    public void addSystemInformation(SystemInformationDTO systemInformationDTO) {
        SystemInformation systemInformation = new SystemInformation();
        systemInformation.setContent(systemInformationDTO.getContent());
        systemInformation.setAdminId(systemInformationDTO.getAdminId());
        systemInformation.setTitle(systemInformationDTO.getTitle());
        systemInformation.setUserId(systemInformationDTO.getUserId());

        int insert = systemInformationDao.insert(systemInformation);
        System.out.println(insert);
    }

    /**
     * 删除系统信息
     */
    @Override
    public void deleteSystemInformation(int id) {
        //删除该条信息
        int i = systemInformationDao.deleteById(id);

        if (i != 1) {
            //删除失败
            throw new SystemException("删除失败");
        }
    }

    /**
     * 提供给管理员模块的更改商品库存方法
     *
     * @param reduceStockDTO
     */
    @Override
    public void reduceStock(ReduceStockDTO reduceStockDTO) {
        commodityService.reduceStock(reduceStockDTO);
    }

    /**
     * 提供给管理员模块的更改价格方法
     *
     * @param changePriceDTO
     */
    @Override
    public void changePrice(ChangePriceDTO changePriceDTO) {
        commodityService.changePrice(changePriceDTO);
    }


    /**
     * 系统公告查询
     *
     * @return
     */
    @Override
    public List<GetSystemAnnouncementVO> getSystemAnnouncement(String title) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(title != null && !title.equals(""), "title", title);
        List<SystemAnnouncement> list = announcementDao.selectList(wrapper);
        List<GetSystemAnnouncementVO> voList = new ArrayList<>();

        for (SystemAnnouncement announcement : list) {
            GetSystemAnnouncementVO vo = new GetSystemAnnouncementVO();
            BeanUtils.copyProperties(announcement, vo);
            voList.add(vo);
        }
        return voList;
    }


    /**
     * 添加系统公告
     */
    @Override
    public void addSystemAnnouncement(AddSystemAnnouncementDTO systemAnnouncementDTO) {
        SystemAnnouncement systemAnnouncement = new SystemAnnouncement();
        systemAnnouncement.setTitle(systemAnnouncementDTO.getTitle());
        systemAnnouncement.setContent(systemAnnouncementDTO.getContent());
        int insert = announcementDao.insert(systemAnnouncement);

        if (insert != 1) {
            //插入失败，抛出异常
            throw new SystemException("插入失败");
        }
    }


    /**
     * 系统消息查询
     *
     * @return
     */
    @Override
    public List<GetSystemInformationVO> getSystemInformation(String title) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title", title);
        List<SystemInformation> list = systemInformationDao.selectList(wrapper);
        List<GetSystemInformationVO> voList = new ArrayList<>();

        for (SystemInformation information : list) {
            GetSystemInformationVO vo = new GetSystemInformationVO();
            BeanUtils.copyProperties(information, vo);
            System.out.println(vo.getSendTime());
            voList.add(vo);
        }
        return voList;
    }


    /**
     * 反馈消息查询
     *
     * @return
     */
    @Override
    public List<GetUserFeedbackVO> getUserFeedback(String title) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(title != null, "title", title);
        List<UserFeedback> list = userFeedbackDao.selectList(wrapper);
        List<GetUserFeedbackVO> voList = new ArrayList<>();

        for (UserFeedback userFeedback : list) {
            GetUserFeedbackVO vo = new GetUserFeedbackVO();
            BeanUtils.copyProperties(userFeedback, vo);
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 更新物流信息
     *
     * @param logisticsInformationDTO
     */
    @Override
    public void updateLogistics(LogisticsInformationDTO logisticsInformationDTO) {
        commodityExchangeRecordService.updateLogisticsInformation(logisticsInformationDTO.getCommodityId(), logisticsInformationDTO.getLogisticsInformation());
    }

    @Override
    public List<Commodity> queryProjectByRange(ProductPriceRangeDTO productPriceRangeDTO) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "name", "price", "remaining_quantity", "launch_time", "url", "history_price", "version")
                .eq(productPriceRangeDTO.getName() != null && !productPriceRangeDTO.getName().isEmpty(),
                        "name", productPriceRangeDTO.getName()).
                between("price", productPriceRangeDTO.getMinPrice(), productPriceRangeDTO.getMaxPrice());
        List<Commodity> commodityList = commodityDao.selectList(queryWrapper);
        return commodityList;
    }

    @Override
    public Commodity selectCommodity(Integer commodityId) {
        return commodityService.getById(commodityId);
    }

    @Override
    public void addMerchandise(AddCommodityDTO addCommodityDTO) {
        commodityService.addMerchandise(addCommodityDTO);
    }

    @Override
    public void deletemerchaindise(int id) {
        commodityService.deleteMerchandise(id);
    }

    @Override
    public void addCommodityExchangeRecord(AddCommodityExchangeRecordDTO addCommodityExchangeRecordDTO) {
        CommodityExchangeRecord commodityExchangeRecord = new CommodityExchangeRecord();
        commodityExchangeRecord.setUserId(addCommodityExchangeRecordDTO.getUserId());
        commodityExchangeRecord.setCommodityId(addCommodityExchangeRecordDTO.getCommodityId());
        commodityExchangeRecord.setState(addCommodityExchangeRecordDTO.getState());
        commodityExchangeRecord.setLogisticsInformation(addCommodityExchangeRecordDTO.getLogisticsInformation());
        commodityExchangeRecordService.save(commodityExchangeRecord);
    }
}
